package com.probox.common.entity.service.product.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.constant.CachePrefix;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.ProductException;
import com.probox.common.entity.domain.product.ProductInfoGroup;
import com.probox.common.entity.domain.product.vo.ProductGroupListVo;
import com.probox.common.entity.domain.product.vo.ProductGroupQuery;
import com.probox.common.entity.domain.product.vo.ProductGroupVo;
import com.probox.common.entity.mapper.product.ProductInfoGroupMapper;
import com.probox.common.entity.service.product.IProductInfoGroupService;
import com.probox.common.entity.vo.proboxService.view.ProboxCardVo;
import com.probox.common.entity.vo.proboxService.view.ProboxServiceItemVo;
import com.probox.common.redis.service.RedisService;
import com.probox.product.api.domain.ProductInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 商品分组信息中间Service业务层处理
 *
 * @date 2021-06-03
 */
@Service
@Transactional
public class ProductInfoGroupServiceImpl extends ServiceImpl<ProductInfoGroupMapper, ProductInfoGroup> implements IProductInfoGroupService {
    @Autowired
    private ProductInfoGroupMapper productInfoGroupMapper;

    @Autowired
    private RedisService redisService;

    private final String sImg = "?imageslim";


    /**
     * 添加商品分组的商品信息
     *
     * @param groupVo 分组信息vo
     */
    private void saveInfoGroup(ProductGroupVo groupVo) {
        List<ProductInfoGroup> productIdList = groupVo.getProductIdList();
        if (CollectionUtil.isNotEmpty(productIdList)) {
            AtomicReference<Long> ind = new AtomicReference<>(0L);
            productIdList.forEach(vos -> {
                ProductInfoGroup productInfoGroup = new ProductInfoGroup();
                productInfoGroup.setDataStatus(DataStatus.valid.getKey());
                productInfoGroup.setGroupUid(groupVo.getGroupUid());
                productInfoGroup.setProductUid(vos.getProductUid());
                productInfoGroup.setProductType(groupVo.getProductType());
                productInfoGroup.setSort(ind.getAndSet(ind.get() + 1));
                productInfoGroup.setIsTuiJian(vos.getIsTuiJian());
                boolean save = this.save(productInfoGroup);
                if (!save) throw new ProductException("操作失败,请稍后再试！");
            });
        }
    }


    /**
     * 新增商品分组信息中间
     *
     * @param groupVo 商品分组信息vo
     * @return 结果
     */
    @Override
    public void insertProductInfoGroup(ProductGroupVo groupVo) {
        saveInfoGroup(groupVo);
    }

    /**
     * 修改商品分组信息中间
     *
     * @param groupVo 商品分组信息中间
     * @return 结果
     */
    @Override
    public void updateProductInfoGroup(ProductGroupVo groupVo) {
        UpdateWrapper<ProductInfoGroup> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("GROUP_UID", groupVo.getGroupUid());
        updateWrapper.eq("product_type", groupVo.getProductType());
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        this.update(updateWrapper);
        List<ProductInfoGroup> productIdList = groupVo.getProductIdList();
        if (CollectionUtil.isNotEmpty(productIdList)) {
            saveInfoGroup(groupVo);
        }
    }

    /**
     * 修改是否推荐
     *
     * @param group
     * @return
     */
    @Override
    public boolean updateRecommend(ProductInfoGroup group) {
        UpdateWrapper<ProductInfoGroup> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("GROUP_UID", group.getGroupUid());
        updateWrapper.eq("PRODUCT_UID", group.getProductUid());
        updateWrapper.eq("PRODUCT_TYPE", group.getProductType());
        updateWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        updateWrapper.set("is_tui_jian", group.getIsTuiJian());
        return this.update(updateWrapper);
    }

    /**
     * 查询商品分组的商品信息
     *
     * @param groupUid 分组ID
     * @return
     */
    @Override
    public List<ProductGroupListVo> queryGroupProductList(Long groupUid) {
        return productInfoGroupMapper.queryGroupProductList(groupUid);
    }


    @Override
    public List<ProboxCardVo> queryGroupCardList(Long groupUid) {
        return productInfoGroupMapper.queryGroupCardList(groupUid);
    }

    @Override
    public List<ProboxServiceItemVo> queryGroupItemList(Long groupUid) {
        return productInfoGroupMapper.queryGroupItemList(groupUid);
    }

    @Override
    public boolean updateProductOffLine(ProductInfoGroup query) {
        UpdateWrapper<ProductInfoGroup> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("product_Type", query.getProductType());
        updateWrapper.eq("PRODUCT_UID", query.getProductUid());
        updateWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        return this.update(updateWrapper);
    }

    /**
     * 查询分组下的所有商品信息
     *
     * @param productGroupQuery
     * @return
     */
    @Override
    public List<ProductInfoVo> queryGroupProductView(ProductGroupQuery productGroupQuery) {
        List<ProductInfoVo> productinfoVos = productInfoGroupMapper.queryGroupProductView(productGroupQuery);
        if (ObjectUtil.isEmpty(productinfoVos)) return productinfoVos;
        List<ProductInfoVo> list = new ArrayList<>();
        productinfoVos.forEach(vo -> {

            // 瘦身图片
            StringBuffer stringBufferCarouse = new StringBuffer();
            String carouselImg = vo.getCarouselImg();
            String[] splitCarouse = carouselImg.split(",");
            for (int i = 0; i < splitCarouse.length; i++) {
                if (0 > splitCarouse[i].indexOf(sImg)) {
                    splitCarouse[i] = splitCarouse[i] + sImg;
                }
                stringBufferCarouse.append("," + splitCarouse[i]);
            }
            vo.setCarouselImg(stringBufferCarouse.toString());
            vo.setCarouselImg(vo.getCarouselImg().substring(1, vo.getCarouselImg().length()));
            list.add(vo);
        });
        return list;
    }

    /**
     * 根据分组uid拿到商品id
     *
     * @param gourpUid
     * @return
     */
    @Override
    public List<Long> selectGroupProUid(Long gourpUid) {
        List<Long> list = new ArrayList<>();
        QueryWrapper<ProductInfoGroup> queryWrapper = Wrappers.query();
        queryWrapper.eq("GROUP_UID", gourpUid);
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        List<ProductInfoGroup> productGroups = productInfoGroupMapper.selectList(queryWrapper);
        if (ObjectUtil.isEmpty(productGroups)) return list;
        productGroups.forEach(group -> {
            list.add(group.getProductUid());
        });
        return list;
    }

    /**
     * 删除分组缓存
     *
     * @param productUid
     */
    @Override
    public void delRedisGroup(Long productUid) {
        QueryWrapper<ProductInfoGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PRODUCT_UID", productUid);
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        List<ProductInfoGroup> productInfoGroups = productInfoGroupMapper.selectList(queryWrapper);
        if (ObjectUtil.isNotEmpty(productInfoGroups)) {
            productInfoGroups.forEach(group -> redisService.deleteObject(CachePrefix.TEMPLATE_PRODUCT_GROUP + group.getGroupUid()));
        }
    }
}
